﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using KegManager;
using KegData;

namespace KegManager
{
    /// <summary>
    /// Interaction logic for KegWindow.xaml
    /// </summary>
    public partial class KegWindow : Window
    {
        public KegeratorDataContext monitor { get; set; }
        public Keg keg { get; set; }
        public System.Linq.IQueryable<Beer> Beers { get; set; }
        public bool IsNew = false;

        public KegWindow(KegeratorDataContext m)
            : this(m, null)
        {
        }

        public KegWindow(KegeratorDataContext m, Keg k)
        {
            monitor = m;
            Beers = monitor.Beers;

            if (k == null)
            {
                k = new Keg();
                k.Filled = DateTime.Now;
                IsNew = true;
            }
            else
            {
                IsNew = false;
            }

            keg = k;

            InitializeComponent();

            // Select the appropriate beer into the combobox
            if(k.Beer != null)
            {
                foreach (Beer b in Beers)
                {
                    if (b.Name == k.Beer.Name)
                    {
                        BeerList.SelectedItem = b;
                    }
                }
            }
        }

        private void FullKegCheck_Checked(object sender, RoutedEventArgs e)
        {
            CurrentVolume.IsEnabled = false;
            CurrentVolume.Text = InitialVolume.Text;
        }

        private void FullKegCheck_Unchecked(object sender, RoutedEventArgs e)
        {
            CurrentVolume.IsEnabled = true;
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            // Tell the main window to cancel this addition.
            this.DialogResult = false;

            // Undo any pending changes on the keg
            monitor.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, keg);

            // the above will fail on new keg

            this.Close();
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            // Set the current beer, since the databinding isn't quite exact.
            keg.Beer = BeerList.SelectedItem as Beer;

            // Add this entry to the data source
            if (IsNew)
            {
                monitor.Kegs.InsertOnSubmit(keg);
            }

            monitor.SubmitChanges();

            this.Close();
        }

        private void buttonNewBeer_Click(object sender, RoutedEventArgs e)
        {
            BeerWindow bwin = new BeerWindow(monitor);

            if(bwin.ShowDialog() == true)
            {
                keg.Beer = bwin.beer;

                Beers = (from be in monitor.Beers
                         select be);

                // Select the appropriate beer into the combobox
                if (bwin.beer != null)
                {
                    foreach (Beer beer in Beers)
                    {
                        if (beer.Name == bwin.beer.Name)
                        {
                            BeerList.SelectedItem = beer;
                        }
                    }
                }
            }
        }
    }
}
